package com.xx.jicheng.controller;

import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.xx.jicheng.entity.Account;
import com.xx.jicheng.service.ConfigService;
import com.xx.jicheng.service.SecurityService;

@Controller
public class LoginController {
	
	@Autowired
	private SecurityService securityService;
	
	@RequestMapping(path="/login",method=RequestMethod.GET)
	public String loginGet(){
		return "/login";
	}
	
	@RequestMapping(path="/login",method=RequestMethod.POST)
	public String login(String principals, String password, HttpSession session) throws LoginException{
		Account account = securityService.login(principals, password);
		if(account == null){
			throw new LoginException();
		}
		session.setAttribute(ConfigService.ACCOUNT_LOGIN, account);
		return "redirect:/index.html";
	}
	
	@RequestMapping(path="/logout")
	public String logout(HttpSession session) {
		session.invalidate();
		return "/login";
	}

}
